<?php
/**
 * Common model for mini-aster
 */
Class miniaster_common extends Model {

	function addUrl($url, $keyword, $src_ip, $user_agent){
		//insert a row with the url and get the id value of the inserted record.
		$this->db->set('url', $url);
		$this->db->insert('urls');
		$inserted_id = $this->db->insert_id();

		//this record is being inserted for tracking purpose.
		//default user id for insertion will be miniaster_a
		$this->db->set('url_id', $inserted_id);
		$this->db->set('ip', $src_ip);
		$this->db->set('user_agent', $user_agent);
		$this->db->insert('url_user_link');

		if(empty($keyword)) {
			//convert the inserted_id value to the base64 and generate the keyword
			$keyword = base_base2base($inserted_id, 10, 64, $iScale=0);
		}
		
		if($this->updateKeyword($url, $keyword)) {
			return $keyword;
		}

		return FALSE;

	} //end function getKeyword()

	function updateKeyword($url, $keyword) {
		//update the keyword in the table 
		$this->db->set('keyword', $keyword);
		$this->db->where('url', $url);
		$this->db->update('urls');

		return TRUE;
		
	} //end function updateKeyword()

	function getUrl($keyword, $src_ip, $user_agent) {
		//Get the id, URL and no of clicks based on keyword
		$this->db->select('id, url, clicks');
		$this->db->where('keyword', $keyword);
		$query = $this->db->get('urls');
		if ($query->num_rows() > 0) {
			$url_row = $query->row();

			//update the no of clicks in the url table
			$this->db->set('clicks', $url_row->clicks + 1);
			$this->db->where('id', $url_row->id);
			$this->db->update('urls');

			//update url access statistics
			$this->db->set('url_id', $url_row->id);
			$this->db->set('ip', $src_ip);
			$this->db->set('user_agent', $user_agent);
			$this->db->insert('url_access_stats');

			return $url_row->url;
		} else {
			return -1;
		}
	} //end function getUrl()

	function getUrlCount() {
		$url_count = $this->db->count_all('urls');
		return $url_count;
	} //end function getUrlCount()

	function getTotalRedirects() {
		$this->db->select_sum('clicks');
		$query = $this->db->get('urls');
		if ($query->num_rows() > 0) {
			$redirects_row = $query->row();
			return $redirects_row->clicks;
		} else {
			return -1;
		}
	} //end function getTotalRedirects()

	function checkDuplicateUrl($url) {
		$this->db->select('keyword');
		$this->db->where('url', $url);
		$query = $this->db->get('urls');
		if ($query->num_rows() >0 ) {
			$url_row = $query->row();
			return $url_row->keyword;
		} else {
			return -1;
		}
	} //end function isUrlDuplicate()

	function isBlocked($keyword) {
		$this->db->where('blocked_kword', $keyword);
		$keyword_count = $this->db->count_all_results('blocked_keywords');
		if($keyword_count == 0) {
			return FALSE;
		} 
		return TRUE;
	} //end function isBlocked()

	function isKeywordAvailable($keyword) {
		$this->db->where('keyword', $keyword);
		$keyword_count = $this->db->count_all_results('urls');
		if($keyword_count == 0) {
			return TRUE;
		} else {
			return FALSE;
		}
		
	} //end function isKeywordAvailable()

	function addUrlWithCustomKeyword($url, $keyword) {
			$this->db->set('url', $url);
			$this->db->set('keyword', $keyword);
			$this->db->insert('urls');

			return TRUE;

	} //end function addUrlWithCustomKeyword()
}
